package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/22 10:55
 */
@RestController
@RequestMapping("/admin/storage")
@CrossOrigin
public class StorageController {
    @Autowired
    private DtsStorageService dtsStorageService;

    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception {

        DtsStorage dtsStorage = dtsStorageService.create(file.getBytes(), file.getOriginalFilename(), file.getSize());

        return ResponseUtil.ok(dtsStorage);
    }

    @GetMapping("/list")
    public Object list(@Param("page") int page, @Param("limit") int limit, @Param("sort") String sort, @Param("order") String order) {
        Page<DtsStorage> dtsStorageList = dtsStorageService.findByPage(page, limit, sort, order);
        Map data = new HashMap<>();
        data.put("total", dtsStorageList.getTotal());
        data.put("items", dtsStorageList.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "删除")

    @RequiresPermissions("admin:storage:delete")
    @PostMapping("/delete")


    public Object delete(@RequestBody DtsStorage dtsStorage) {
        dtsStorage.setDeleted(true);
        dtsStorageService.deleteById(dtsStorage);
        return ResponseUtil.ok();
    }

}
